/**
 * Collaborative Story Development Web Application (CSDApp) Copyright Anton
 * Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Connection;
import java.util.List;

/**
 * This class handles the user initialed requests for all post type models. It
 * provides functionality specific to post types.
 *
 * @author Anton
 */
public class PostController extends ResourceController {

    protected final int STATE_PRIOR_VERSION = 8;
    protected final int STATE_DRAFT = 7;

    // protected PostHistoryController postHistoryController;
    public PostController(Connection conn) {
        super(conn);
    }

    /**
     * This method handles get requests. It accepts the primaryResouce and
     * primaryAction as variables to allow routing from the handleGetRequest
     * method.
     *
     * @param request the request object
     * @return an updated request object sufficient to complete the request.
     */
    @Override
    public CsdwaRequest handleGetRequest(CsdwaRequest request) {
        return request;
    }

    /**
     * This method primarily handles form related requests.
     *
     * @param request the request object
     * @return an updated request object sufficient to complete the request.
     */
    @Override
    public CsdwaRequest handlePostRequest(CsdwaRequest request) {
        throw new UnsupportedOperationException("Not supported.");
    }

    /**
     * This method provides an entry hook, for setup purposes before calling the
     * core handler.
     *
     * @param request the request object
     * @return an updated request object sufficient to complete the request.
     */
    @Override
    protected CsdwaRequest requestStartHandle(CsdwaRequest request) {
        request = super.requestStartHandle(request);
        //post workflow restrictions will potentially be enforced here.

        return request;
    }

    /**
     * The core handler of the request.
     *
     * @param request the request object
     * @return an updated request object with the initial logic of the request
     * complete
     */
    @Override
    protected CsdwaRequest requestCoreHandle(CsdwaRequest request) {
        request = super.requestCoreHandle(request);
        return request;
    }

    /**
     * This method acts as an end hook into the request processing. It attaches
     * the visual markers to posts indicating the state of the post according to
     * the particular user.
     *
     * @param request the request with it's initial logic completed and now on
     * the return path to be displayed.
     * @return the request with the posts in the beans map updated to contain
     * derived values indicating their state.
     */
    @Override
    protected CsdwaRequest requestEndHandle(CsdwaRequest request) {
        request = super.requestEndHandle(request);
        request = this.getResourceRequestDelegator().getUserHistoryManager().processUserPostHistory(request);
        return request;
    }

    /**
     * This method deletes all posts in the provided dao.
     *
     * @param dao the dao object containing the posts to delete
     * @return true if all the posts were deleted successfully and false if a
     * delete operation failed.
     */
    public boolean deleteDependants(CsdwaDao dao) {
        boolean canContinue = false;
        List<Object> beans = dao.getBeans();
        if (beans.isEmpty() == false) {
            int continueDelete = 1;
            int i = 0;
            int plotsSize = beans.size();
            while (continueDelete == 1 && i < plotsSize) {
                continueDelete = dao.deleteBean(beans.get(i));
                i++;
                if (continueDelete == 1) {
                    canContinue = true;
                }
            }
        } else {
            canContinue = true;
        }
        return canContinue;
    }
}
